﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BySoft.Web.Utility;
using System.Data;
using BySoft.Server.Business;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common;
using BySoft.Server.Common.Enum;
using BySoft.Server.Consts;
using BySoft.Web.Controls;

namespace BySoft.Web.Site.SysManage
{
    public partial class UserList : AuthBasePage
    {
        #region 变量定义
        int m_count = 0;
        public string m_dispaly = "none";
        #endregion

        #region 属性定义

        #endregion

        #region 获取页面传参

        #endregion

        #region 初始化及绑定控件
        private void BindData(int pageIndex)
        {
            DataTable dtUser = SysUser.GetAllUserInfo(txtUserName.Text.Trim(), txtUserID.Text.Trim(), ddlStatus.SelectedItem.Value,
               pageIndex, pcData.PageSize, out m_count);
            pcData.RecordCount = m_count;
            if (dtUser.Rows.Count > 0)
            {
                foreach (DataRow dr in dtUser.Rows)
                {
                    string IsGeneral = dr["IsGeneral"].ToString().Trim();
                    if (IsGeneral.Equals("1")) 
                    {
                        dr["IsGeneral"] = "是";
                    }
                    else
                    {
                        dr["IsGeneral"] = "否";
                    }
                    dr["DepartmentID"] = SysParameter.GetParameterName("Department", dr["DepartmentID"].ToString().Trim());
                }
                grdResult.DataSource = dtUser;
                grdResult.DataBind();
                //pcData.Visible = true;

                pcData.CurrentIndex = pageIndex;
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dtUser);
                //pcData.Visible = false;
            }
        }
        #endregion

        #region 控件事件
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                UIHelper.InsertFirstItem(ddlStatus);

                BindData(0);
            }
        }



        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData(0);
        }

        protected void pcData_PageIndexClick(object sender, PageIndexClickEventArgs e)
        {
            BindData(e.PageIndex);
        }

        protected void grdResult_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            SysUserEntity oSysUser = SysUser.GetSysUserByID(e.CommandArgument.ToString());
            string strCode = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "ChangeStauts":
                    if (SysUser.ChangeUserStatus(e.CommandArgument.ToString(), oSysUser.Status == "1" ? "0" : "1") > 0)
                    {
                        BindData(grdResult.PageIndex);
                    }
                    break;
                case "ResetPwd":
                    //string tempPwd = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 8);
                    //string tempPwd = "123456";

                    //密码规则 2个字母+特殊符+两个字母+特殊符+四位随机数 tina.su modify 2010-8-8
                    string tempPwd = CommonMethod.GetRandomPassword();

                    //string sContent = "您好！";
                    //sContent += "\r\n  您的强生ECTS系统密码已重置。";
                    //sContent += "\r\n  您当前的登录账号是：" + oSysUser.Userloginid + "，密码：" + tempPwd + "。 ";
                    //sContent += "\r\n  请点击" + this.Page.Request.Url.ToString().Split('/')[0] + "//" + this.Page.Request.Url.ToString().Split('/')[2] + "/Login.aspx 登录系统并修改密码！";
                    //sContent += "\r\n";
                    //sContent += "\r\n=========================";
                    //sContent += "\r\nECTS 管理员";
                    //try
                    //{

                    //Regex reg = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
                    //if (reg.IsMatch(oSysUser.Mail))
                    //{

                    //    CommonMethod.SendMail("您的强生ECTS系统密码已重置！", sContent, oSysUser.Mail);
                    //}

                    if ((SysUser.ResetPwd(e.CommandArgument.ToString(), CommonMethod.Encrypt(tempPwd))) > 0)
                    {
                        UIHelper.AlertMessage(this.Page, Message.PwdHasChanged + "新密码为" + tempPwd);
                        BindData(grdResult.PageIndex);
                    }
                    break;
                case "del":
                    if(SysUser.IsUser(strCode))
                    {
                        UIHelper.AlertMessage(this.Page, "此用户已经被引用，不能删除！");
                        return;
                    }
                    SysUser.Del(strCode);
                    BindData(grdResult.PageIndex);
                    break;
                default:
                    break;
            }
        }

        protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                #region 删除绑定
                ImageButton ibtn = (ImageButton)e.Row.FindControl("ibtnDel");
                Button btnDel = (Button)e.Row.FindControl("btnDel");
                if (ibtn != null)
                {
                    ibtn.OnClientClick = "ShowTopConfirm('提示信息','" + Message.ConfirmDelete + "','confirmCallback(\\'" + btnDel.ClientID + "\\')');return false;";
                }
                #endregion

                if (!e.Row.Cells[2].Text.Equals("&nbsp;"))
                {
                    e.Row.Cells[2].Text = BaseEnum.StatusList()[e.Row.Cells[2].Text].ToString();
                }
                if (grdResult.DataKeys[e.Row.RowIndex].Values["STATUS"].ToString().Equals("1"))
                {
                    ((ImageButton)e.Row.FindControl("lbtnChangeStauts")).ToolTip = "停用";
                    ((ImageButton)e.Row.FindControl("lbtnChangeStauts")).ImageUrl = "../../App_Themes/Common_CSS/Images/Disable.gif";
                }
                else
                {
                    ((ImageButton)e.Row.FindControl("lbtnChangeStauts")).ToolTip = "启用";
                    ((ImageButton)e.Row.FindControl("lbtnChangeStauts")).ImageUrl = "../../App_Themes/Common_CSS/Images/start.gif";
                }

            }
        }
        #endregion

        #region 本地方法
        public string SetFormat(object param)
        {
            return HttpUtility.UrlEncode(param.ToString());
        }
        #endregion

        #region 校验方法
        #endregion

    }
}
